<?php
/**
 * Created by PhpStorm.
 * User: hwy
 * Date: 2020/7/13
 * Time: 8:10
 */

namespace App\Http\Controllers\Index;


use App\Handlers\wechat\MessageHandler;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use EasyWeChat\Kernel\Messages\Article;
use Log;
use EasyWeChat\Factory;

class WeiXinController extends Controller
{
    public function index(){
//        $this->returnWxArticle();
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志
        $app = app('wechat.official_account');
        $app->server->push(function($message){
//            Log::info($message['FromUserName']);
//            Log::info($message['MsgType']);
//            return "欢迎关注 hwy！";

            $this->returnWxArticle();
        });
        $app->server->push(MessageHandler::class);
        return $app->server->serve();
    }


    public function returnWxArticle($title = 'a', $author = 'hwy', $content = 'content', $thumb_media_id = '', $digest = '', $source_url = ''){

        $app = app('wechat.official_account');
        $openId = 'ogB-LwbqVudImvoN_hyruvh9qcwQ';

        $text = Carbon::now()->toDateTimeString().'  增加文章【'.$title.'】';
        $app->broadcasting->previewText($text, $openId);
    }

}
